//
//  WBTitleButton.swift
//  Weibo
//
//  Created by kobe on 2016/12/25.
//  Copyright © 2016年 kobe. All rights reserved.
//

import UIKit

class WBTitleButton: UIButton {
    
    init(title:String?) {
        super.init(frame: CGRect())
        
        if title == nil {
            setTitle("首页", for: [])
        } else {
            setTitle(title! + "", for: [])
            setImage(UIImage(named:"navigationbar_arrow_down"), for: [])
            setImage(UIImage(named:"navigationbar_arrow_up"), for: .selected)

        }
        titleLabel?.font = UIFont.boldSystemFont(ofSize: 17)
        setTitleColor(UIColor.darkGray, for: [])
        sizeToFit()
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //重新布局子视图
    override func layoutSubviews() {
        super.layoutSubviews()
        
        
        
        //判断label和imageView是否同时存在
        guard let titleLabel = titleLabel,
                let imageView = imageView else {
                    
            return
        }
        
        titleLabel.frame = titleLabel.frame.offsetBy(dx: -imageView.bounds.width, dy: 0)
        
        imageView.frame = imageView.frame.offsetBy(dx: titleLabel.bounds.maxX, dy: 0)

        
    }

}
